package com.takeshequ.feeds;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.AnnotationUtils;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.annotation.view.ViewInject;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.takeshequ.BasicActivity;
import com.takeshequ.Constants;
import com.takeshequ.PullAndLoadListView;
import com.takeshequ.R;
import com.takeshequ.homepage.HomePageActivity;
import com.utils.HttpClientUtils;
import com.utils.HttpClientUtils.HttpResponseHandler;

public class FeedsActivity extends BasicActivity {
	private static int pageNumber = 1;

	@ViewInject(id = R.id.feeds_list_view_process)
	private View processView;

	@ViewInject(id = R.id.feeds_list_view_nomessage)
	private View nomessageView;

	@ViewInject(id = R.id.feeds_list_view)
	private PullAndLoadListView listView;

	private FeedsAdapter adapter;

	private static ArrayList<FeedsListItem> items;
	static {
		items = new ArrayList<FeedsListItem>();
		items.add(new FeedsListItem());

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feeds);
	}

	@Override
	protected void onResume() {
		super.onResume();
//		listView.setOnRefreshListener(new OnRefreshListener() {
//			@Override
//			public void onRefresh() {
//				listView.postDelayed(new Runnable() {
//					@Override
//					public void run() {
//						getListItems(new RefreshAsyncHttpResponseHandler());
//						listView.onRefreshComplete();
//					}
//				}, 2000);
//			}
//		});
//		adapter = new FeedsAdapter(this.getLayoutInflater());
//		listView.setAdapter(adapter);
//		getListItems(new RefreshAsyncHttpResponseHandler());
	}

	private class ViewHolder {
		public TextView nickName, content, distance, time, loveNumber,
				chatNumber, age, sourceFrom;
		@ViewInject(id=R.id.feeds_list_view_head_img,click="test")
		public ImageView headImage;
		
		public void test(View view){
			System.out.println("大sd萨大使");
		}
	}

	class FeedsAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		public FeedsAdapter(LayoutInflater inflater) {
			this.inflater = inflater;
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (items.size() > position) {
				View view = viewMap.get(position);
				final ViewHolder holder;
				if (view == null) {
					view = inflater.inflate(R.layout.feeds_list_item, parent,
							false);
					holder = new ViewHolder();
					AnnotationUtils.initView(FeedsActivity.this, view, holder);
//					holder.picImage = (ImageView) view
//							.findViewById(R.id.feeds_list_view_pic);
//					holder.nickName = (TextView) view
//							.findViewById(R.id.feeds_list_view_nickName);
//					holder.nickName.getPaint().setFakeBoldText(true);
//					holder.headImage = (ImageView) view
//							.findViewById(R.id.feeds_list_view_head_img);
//					holder.distance = (TextView) view
//							.findViewById(R.id.feeds_list_item_distance);
//					holder.content = (TextView) view
//							.findViewById(R.id.feeds_list_view_content);
//					holder.time = (TextView) view
//							.findViewById(R.id.feeds_list_view_time);
//					holder.age = (TextView) view
//							.findViewById(R.id.feeds_item_age);
//					holder.genderImage = (ImageView) view
//							.findViewById(R.id.fedds_item_gender_img);
//					holder.chatNumber = (TextView) view
//							.findViewById(R.id.feeds_list_item_chat_number);
//					holder.loveNumber = (TextView) view
//							.findViewById(R.id.feeds_list_item_love_number);
//					holder.sourceFrom = (TextView) view
//							.findViewById(R.id.feeds_list_item_source);
//					holder.loveImage = (ImageView) view
//							.findViewById(R.id.feeds_list_view_love_btn);
					view.setTag(holder);
					viewMap.put(position, view);
				} else {
					holder = (ViewHolder) view.getTag();
				}

//				final FeedsListItem feed = items.get(position);
//				holder.nickName.setText(feed.getNickName());
//				FinalBitmap.create(FeedsActivity.this).display(
//						holder.headImage, feed.getHeadImage());
//				holder.content.setText(getSpannableString(feed.getCont()));
//				holder.distance.setText(feed.getDistance() + " KM");
//				holder.time.setText(feed.getCreateDateString());
//				displayImage(holder.picImage, feed.getImage());
//				showGender(holder.genderImage, feed.getSex());
//				holder.headImage.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//						Constants.selectMemberId = feed.getMemberId();
//						startActivity(new Intent(FeedsActivity.this,
//								HomePageActivity.class));
//					}
//				});
//				holder.age.setText(feed.getAge());
//				holder.chatNumber.setText(feed.getCommentCount());
//				holder.loveNumber.setText(feed.getPraiseCount());
//				holder.sourceFrom.setText(feed.getSourcefrom());
//				view.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//						Intent intent = new Intent(FeedsActivity.this,
//								FeedsDetailActivity.class);
//						intent.putExtra("behaviorId", feed.getBehaviorId());
//						startActivity(intent);
//					}
//				});
//
//				holder.loveImage.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//
//						Map<String, String> params = new HashMap<String, String>();
//						params.put("nickName", Constants.member.getNickName());
//						params.put("headImage", Constants.member.getHeadImage());
//						params.put("behaviorId", feed.getBehaviorId());
//						params.put("cont", "");
//						params.put("commentType", "2");
//						HttpClientUtils.post("/praiseBehavior", params,
//								new HttpResponseHandler() {
//
//									@Override
//									public void handler(String response) {
//										try {
//											JSONObject jsonObject = new JSONObject(
//													response);
//											holder.loveNumber.setText(jsonObject
//													.getString("totalCount"));
//											Toast.makeText(FeedsActivity.this,
//													"已赞", 2000).show();
//										} catch (JSONException e) {
//											e.printStackTrace();
//										}
//
//									}
//								});
//					}
//				});
				return view;
			}

			return null;
		}
	}

	class RefreshAsyncHttpResponseHandler extends AsyncHttpResponseHandler {

		@Override
		public void onStart() {
			super.onStart();
			System.out.println("开始");
			processView.setVisibility(View.VISIBLE);
			nomessageView.setVisibility(View.VISIBLE);
		}

		@Override
		public void onSuccess(String response) {
			System.out.println("结束");
			try {
				JSONObject jsonObject = new JSONObject(response);
				if (jsonObject.getJSONArray("behaviorList").length() > 0) {
					items = gson.fromJson(jsonObject.getString("behaviorList"),
							new TypeToken<List<FeedsListItem>>() {
							}.getType());
					adapter.notifyDataSetChanged();
					nomessageView.setVisibility(View.GONE);
				}
				processView.setVisibility(View.GONE);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	private void resetPageNumber() {
		pageNumber = 1;
	}

	private void pageNumberAddOne() {
		pageNumber = pageNumber + 1;
	}

	private void pageNumberReduceOne() {
		if (pageNumber > 1) {
			pageNumber = pageNumber - 1;
		}
	}
}
